package com.example.stars_project_android;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.stars_project_android.entity.SpaceTime;
import com.example.stars_project_android.index.Index;
import com.example.stars_project_android.util.Constant;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Iterator;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * 欢迎界面
 */
public class HelloActivity extends AppCompatActivity {

    private TextView textUser, textDetail;
    private LinearLayout next;
    private int code;   //返回的状态码
    private String msgCon;  //返回的信息
    private Handler myHandler = new Handler(Looper.myLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            //处理子线程发送的消息
            String result = (String) msg.obj;
            try {
                JSONObject jsonObject = new JSONObject(result);
                code = jsonObject.getInt("code");
                msgCon = jsonObject.getString("msg");
                Log.e("状态码", code + "");
                Log.e("返回的提示信息：", msgCon);
                if (code == 200) {
                    if (Constant.roomList.size() != 0){
                        Constant.roomList.clear();
                    }
                    //处理收到数据
                    JSONObject jsonObject1 = jsonObject.getJSONObject("date");
                    JSONArray jsonArray = jsonObject1.getJSONArray("content");
                    for (int i = 0; i < jsonArray.length(); i++){
                        JSONObject jsonObject2 = jsonArray.getJSONObject(i);
                        SpaceTime spaceTime = new SpaceTime();
                        spaceTime.setRoomId(jsonObject2.getString("roomId"));
                        spaceTime.setCreateTime(jsonObject2.getString("createTime"));
                        spaceTime.setRoomName(jsonObject2.getString("roomName"));
                        spaceTime.setUserCountMax(jsonObject2.getInt("userCountMax"));
                        spaceTime.setUserCountCurrent(jsonObject2.getInt("userCountCurrent"));
                        spaceTime.setUserPhone(jsonObject2.getString("userPhone"));
                        spaceTime.setRoomDescribe(jsonObject2.getString("roomDescribe"));
                        spaceTime.setRoomAttribute(jsonObject2.getString("roomAttribute"));
                        spaceTime.setRoomState(jsonObject2.getString("roomState"));
                        Constant.roomList.add(spaceTime);
                    }
                }
                Intent intent = new Intent(HelloActivity.this, Index.class);
                startActivity(intent);
            }catch (JSONException e) {
                e.printStackTrace();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hello);

        //绑定控件对象
        setView();
        //设置数据
        setData();
        //设置点击事件
        next.setOnClickListener(view->{
            //获取网络连接，得到聊天时空的数据
            getConn();
//            Intent intent = new Intent(this,im.zego.gomovie.client.movielist.JoinMovieRoomActivity.class);
//            startActivity(intent);
        });

    }

    /**
     * 获取网络连接
     */
    private void getConn() {
        //向后端传递获取聊天房间的信息

        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url("http://192.168.1.7:8080/room/findRoomByAttribute?roomAttribute=聊天时空")
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("okhttp","请求失败");
                e.printStackTrace();
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.e("okhttp","请求成功");
                //处理后端的数据
                response = okHttpClient.newCall(request).execute();
                String responseDate = response.body().string();
                Log.e("返回的数据",responseDate);
                //数据源发送改变，需要更新界面，使用Handler
                Message message = new Message();
                message.obj = responseDate;
                myHandler.sendMessage(message);//发送到主线程的消息队列
            }
        });
    }

    private void setData() {
        textUser.setText("Hi, "+ Constant.starName);
        textDetail.setText(Constant.starName+"是一颗位于赤经\n"+Constant.longitude+",赤纬"+Constant.latitude+"\n的小星星，距地球"+Constant.distance+"光年");
    }

    private void setView() {
        textUser = findViewById(R.id.text_user);
        textDetail = findViewById(R.id.text_detail);
        next = findViewById(R.id.next);
    }
}